import { defineStore } from 'pinia';
import { StoreName } from "./store-name";

import zhCn from 'element-plus/es/locale/lang/zh-cn';
import en from 'element-plus/es/locale/lang/en';
export const locale: any = {
  zh: zhCn,
  en: en
}

import JSONzh from '@/assets/i18n/zh.json'
import JSONen from '@/assets/i18n/en.json'
export const messages: any = {
  zh: JSONzh,
  en: JSONen
}

import { createI18n } from 'vue-i18n'
const language = localStorage.getItem(StoreName.I18N) ? JSON.parse(localStorage.getItem(StoreName.I18N) || '').locale || 'zh' : 'zh';
export const i18n = createI18n({
  legacy: false, // 必须指定 legacy 为 false
  locale: language,
  fallbackLocale: 'zh',
  messages
})

// 导入默认的语言
import { VxeUI, type VxeGlobalI18nLocale } from 'vxe-table';
import zhCN from 'vxe-pc-ui/lib/language/zh-CN'
import enUS from 'vxe-pc-ui/lib/language/en-US'

export const useI18nStore = defineStore(StoreName.I18N, {
  state: () => {
    return {
      locale: 'zh'
    };
  },
  getters: {
    getI18nList: () => Object.keys(messages),
  },
  actions: {
    setLocale(locale: string) {
      this.locale = locale;
      i18n.global.locale.value = locale;
      this.setVxeUII18n();
    },
    setVxeUII18n() {
      let key: VxeGlobalI18nLocale = 'zh-CN';
      let value = zhCN;
      switch (this.locale) {
        case 'en':
          key = 'en-US';
          value = enUS;
          break;
        default:
          key = 'zh-CN';
          value = zhCN;
      }
      VxeUI.setI18n(key, value)
      VxeUI.setLanguage(key)
    }
  },
  persist: true
});